package com.javafactroy.kunlong.domain.deptemp.factroy;

import com.javafactroy.kunlong.data.dto.VDeptApiDto;
import com.javafactroy.kunlong.domain.deptemp.agg.DeptTreeAgg;
import com.javafactroy.kunlong.domain.deptemp.repository.DeptTreeAggRepository;
import com.javafactroy.kunlong.domain.generalright.agg.UserRightAgg;
import com.javafactroy.kunlong.domain.generalright.repository.UserRightRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class DeptTreeAggFactroy {
    @Autowired
    UserRightRepository userRightRepository;

    @Autowired
    DeptTreeAggRepository deptTreeAggRepository;

    public void makeRightFlag(DeptTreeAgg agg, UserRightAgg userRightAgg) {
        agg.getDeptApiDtos().forEach(
                i ->  makeRight(i,userRightAgg)
        );
    }

    void makeRight(VDeptApiDto deptApiDto, UserRightAgg userRightAgg) {
        deptApiDto.setHasRight(userRightAgg.deptHasRight(deptApiDto.getDeptId()));
        deptApiDto.getChild().forEach(i->makeRight(i,userRightAgg));
    }

    public DeptTreeAgg loadRight(Long userId) {
        UserRightAgg userRightAgg = userRightRepository.load(userId);
        // userRightAgg.getUserDepts();
        DeptTreeAgg agg = deptTreeAggRepository.load();
        makeRightFlag(agg, userRightAgg);
        return agg;
    }
}
